var config = require('config-lite');

module.exports = function(sequelize, DataTypes) {
  var Staff = sequelize.define('staff', {
    uuid: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true
    },
    no: {
      type: DataTypes.INTEGER
    },
    name: {type: DataTypes.STRING(50)},
    name_en: {type: DataTypes.STRING(50)},
    type: {
      type: DataTypes.BOOLEAN,
      defaultValue: 1,
      get: function () {
        return this.getDataValue('type') * 1;
      }
    },
    sex: {
      type: DataTypes.BOOLEAN,
      defaultValue: 1,
      get: function () {
        return this.getDataValue('sex') * 1;
      }
    },
    idtype: {
      type: DataTypes.BOOLEAN,
      defaultValue: 1,
      get: function () {
        return this.getDataValue('idtype') * 1;
      }
    },
    idno: {type: DataTypes.STRING(50)},
    organization: {type: DataTypes.STRING(50)},
    organization_en: {type: DataTypes.STRING(50)},
    position_uuid: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4
    },
    avatar: {
      type: DataTypes.STRING
    },
    avatar_url: {
      type: DataTypes.VIRTUAL,
      get: function() {
        if (this.getDataValue('avatar')) {
          var imgUrl = config.ssl ? 'https://' : 'http://' + config.host;
          return imgUrl + config.filePath + this.getDataValue('avatar');
        } else {
          return '';
        }
      }
    },
    event_uuid: {
      type: DataTypes.UUID
    },
    belongto_area_uuid: {
      type: DataTypes.UUID
    },
    status: {
      type: DataTypes.BOOLEAN,
      defaultValue: 0,
      get: function () {
        return this.getDataValue('status') * 1;
      }
    }
  }, {
    // 是否需要增加created_at、updated_at、deleted_at字段
    timestamps: true,
    underscored: true,
    // 设置paranoid为true（此种模式下，删除数据时不会进行物理删除，而是设置deleted_at为当前时间
    paranoid: true
  });

  Staff.associate = function(models) {
    // Using additional options like CASCADE etc for demonstration
    // Can also simply do Task.belongsTo(models.User);
    // User.belongsTo(models.xxx, {
    //   // onDelete: 'CASCADE',
    // });
    Staff.belongsTo(models.position, {
      // onDelete: 'CASCADE',
    });

    Staff.belongsTo(models.event, {
      // onDelete: 'CASCADE',
    });

    Staff.belongsTo(models.area, {
      // onDelete: 'CASCADE',
      as: 'belongto_area',
      foreignKey: {
        name: 'belongto_area_uuid'
      }
    });
  };

  return Staff;
};
